package com.hushao.aimi.service.impl;

import com.hushao.aimi.DateUtil;
import com.hushao.aimi.common.PageVo;
import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.bo.RecommendBo;
import com.hushao.aimi.entity.AimiProduct;
import com.hushao.aimi.entity.AimiRecommend;
import com.hushao.aimi.repository.RecommendRepository;
import com.hushao.aimi.service.ProductService;
import com.hushao.aimi.service.RecommendService;
import com.hushao.aimi.vo.RecommendVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;


/**
 * @description: 推荐服务
 * @author: 胡少
 * @create: 2018-06-21 15:28
 **/
@Slf4j
@Service
public class RecommendServiceImpl implements RecommendService {

    @Autowired
    private RecommendRepository recommendRepository;
    @Autowired
    private ProductService productService;


    @Override
    public ServerResponse listByProductCodeLike(String productCode, Pageable pageable) {
        productCode="%"+productCode+"%";
        Page<Object> page=recommendRepository.listByProductCodeLike(productCode,pageable);
        List<RecommendVo> list=page.getContent().stream()
                .map(RecommendBo::new)
                .map(rb->new RecommendVo(rb.getRecommendId(),
                        rb.getProductId(),
                        rb.getProductCode(),
                        rb.getProductName(),
                        rb.getRecommendCode(),
                        rb.getRecommendImage(),
                        rb.getRecommendTitle(),
                        rb.getSortOrder(),
                        rb.getRecommendStatus(),
                        DateUtil.getDateTimeOfTimestamp(rb.getCreateTime()),
                        DateUtil.getDateTimeOfTimestamp(rb.getUpdateTime()),
                        rb.getComment())).collect(Collectors.toList());
        PageVo pageVo=new PageVo(page);
        Map<String,Object> map=new ConcurrentHashMap<>(2);
        map.put("page",pageVo);
        map.put("recommends",list);
        return ServerResponse.Success(map);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServerResponse addRecommend(RecommendVo recommendVo) throws Exception {
        Optional<AimiProduct> optional=productService.getProductCode(recommendVo.getProductCode());
        String productId=optional.map(AimiProduct::getId).orElseThrow(()->new RuntimeException("没有此商品！"));
        AimiRecommend recommend=new AimiRecommend(productId,
                recommendVo.getRecommendCode(),
                recommendVo.getRecommendImage(),
                recommendVo.getRecommendTitle(),
                recommendVo.getSortOrder(),
                recommendVo.getRecommendStatus(),
                recommendVo.getComment());
        recommend=recommendRepository.save(recommend);
        log.info("添加商品推荐位成功：{}",productId);

        recommendVo.setRecommendId(recommend.getRecommendId());
        recommendVo.setCreateTime(DateUtil.getDateTimeOfTimestamp(recommend.getCreateTime()));
        recommendVo.setUpdateTime(DateUtil.getDateTimeOfTimestamp(recommend.getUpdateTime()));
        return ServerResponse.Success("添加推荐位成功",recommendVo);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServerResponse delRecommend(Integer recommendId) throws Exception {
        recommendRepository.deleteById(recommendId);
        log.info("删除商品推荐位成功：{}",recommendId);
        return ServerResponse.Success("删除商品推推荐位成功！");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ServerResponse updateRecommend(RecommendVo vo)throws Exception {
        Optional<AimiRecommend> optional=recommendRepository.findById(vo.getRecommendId());
       AimiRecommend recommend=optional.map(o->{
           o.setProductId(vo.getProductId());
           o.setRecommendCode(vo.getRecommendCode());
           o.setRecommendTitle(vo.getRecommendTitle());
           o.setRecommendImage(vo.getRecommendImage());
           o.setSortOrder(vo.getSortOrder());
           o.setRecommendStatus(vo.getRecommendStatus());
           o.setComment(vo.getComment());
           return o; }
           ).orElseThrow(()->new RuntimeException("错误！"));

        recommendRepository.flush();
        log.info("更新商品推荐位成功，{}",recommend);
        return ServerResponse.Success("更新成功！");
    }
}
